package com.mf.page;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.mf.R;
import com.mf.mpos.pub.Controler;


public class MsgPage extends Activity{
	private TextView textView;
	private Button rbtn;
	private Button lbtn;
	private Bundle param;
	public static final String COMMPAGE_UPDATE_ACTION = "com.CommMsgActivity.UpdateAction";
	public static final String COMMPAGE_CLOSE_ACTION = "com.CommMsgActivity.CloseAction";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_msg_page);				
		rbtn = (Button)findViewById(R.id.rightbtn);
		lbtn = (Button)findViewById(R.id.leftbtn);
		rbtn.setOnClickListener( new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub	
				//判断是否有需要响应的取消键函数，如果有则，执行
				onRBtnCliecked();
			}
		});
		
		lbtn.setOnClickListener( new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub	
				//判断是否有需要响应的取消键函数，如果有则，执行
				onLBtnCliecked();
			}
		});
		
		param = this.getIntent().getExtras();
		
		onRefrushParam();
		
		registerReceiver(updateReceiver, new IntentFilter(COMMPAGE_UPDATE_ACTION));
		registerReceiver(updateReceiver, new IntentFilter(COMMPAGE_CLOSE_ACTION));
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(updateReceiver);
	}
	
	protected void onRefrushParam() {
		// TODO Auto-generated method stub
		//设置要显示的消息
		textView = (TextView) findViewById(R.id.textView1);
		textView.setText(param.getString("msg"));
		
		//处理右边按钮，有文本就显示，否则隐藏
		String rightbtnText = param.getString("rightBtnText");
		
		if(rightbtnText.length() > 0)
		{
			rbtn.setVisibility( View.VISIBLE  );
			rbtn.setText(rightbtnText);
			rbtn.requestFocus();
		}
		else
		{
			rbtn.setVisibility( View.GONE  );
		}		
		
		//处理左边按钮，有文本就显示，否则隐藏
		String leftBtnText = param.getString("leftBtnText");
		
		if(leftBtnText.length() > 0)
		{
			lbtn.setVisibility( View.VISIBLE  );
			lbtn.setText(leftBtnText);
			lbtn.requestFocus();
		}
		else
		{
			lbtn.setVisibility( View.GONE  );
		}	
	}
	
	protected void onRBtnCliecked()
	{	
		//判断按键是否有取消回调
		IPageCallback btnProc = (IPageCallback)param.getSerializable("pageCallback");

		if(btnProc != null)
		{	
			//如果取消执行失败，则不关闭本页面
			if(!btnProc.ButtonClickedFun())
			{
				return;
			}
			
			btnProc.SetExitReason(IPageCallback.OK);
		}
	}
	
	protected void onLBtnCliecked()
	{	
		//判断是否有界面回调，有的话设置回调为确认键按下
		IPageCallback btnProc = (IPageCallback)param.getSerializable("pageCallback");

		if(btnProc != null)
		{	
			//如果取消执行失败，则不关闭本页面
			
			btnProc.SetExitReason(IPageCallback.CANCEL);
		}
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Controler.CancelComm();
		if(lbtn.getVisibility() == View.VISIBLE)
		{
			onLBtnCliecked();
		}
	}
	
    /*
     * 接收动态注册广播的BroadcastReceiver，用来更新内容
     */
    private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        	if(action == COMMPAGE_UPDATE_ACTION)
        	{
        		Log.v("uninpay", "MsgPage receive update_action");
        		param = intent.getExtras();
        		onRefrushParam();
        	}
        	else if(action == COMMPAGE_CLOSE_ACTION)
        	{
        		Log.v("uninpay", "MsgPage receive close_action");
        		finish();
        	}
        }
    };
}
